//
//  YREmotionPageView.swift
//  新浪微博
//
//  Created by Viper on 16/7/5.
//  Copyright © 2016年 Viper. All rights reserved.
//

import UIKit
private let EmotionPageCellId = "emotionPageCellId"
class YREmotionPageView: UICollectionView {


    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置视图
    
    private func setupUI(){
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        //设置代理
        dataSource = self
        
        registerClass(YREmotionViewCell.self, forCellWithReuseIdentifier: EmotionPageCellId)
        //设置layout
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        
        //设置layout的item
        layout.itemSize = CGSize(width: SCREENW, height: 216 - 37)
        
        //设置间距
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        //设置滚动方向
        layout.scrollDirection = .Horizontal
        
        
    }
    
    
}

extension YREmotionPageView : UICollectionViewDataSource{
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        //
        return YREmotionTools.sharedTools.allEmotions.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //返回多少页
        return YREmotionTools.sharedTools.allEmotions[section].count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(EmotionPageCellId, forIndexPath: indexPath) as! YREmotionViewCell
        
        cell.indextPath = indexPath     
//        cell.backgroundColor = RandomColor()
        //给cell设置
        cell.emotions  = YREmotionTools.sharedTools.allEmotions[indexPath.section][indexPath.item]
        
        return cell
    }
    
}

